package com.sanhe.a448.activity.work;

import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.sanhe.a448.R;
import com.sanhe.a448.databinding.ActivityTimeBinding;
import com.sanhe.a448.event.CetClean;
import com.sanhe.a448.event.CetDayEvent;
import com.sanhe.a448.event.CetHourEvent;
import com.sanhe.a448.event.CetMinEvent;
import com.sanhe.a448.event.Event;
import com.sanhe.a448.event.HspClean;
import com.sanhe.a448.event.HspDayEvent;
import com.sanhe.a448.event.HspHourEvent;
import com.sanhe.a448.event.HspMinEvent;
import com.sanhe.a448.event.RetClean;
import com.sanhe.a448.event.RetDayEvent;
import com.sanhe.a448.event.RetHourEvent;
import com.sanhe.a448.event.RetMinEvent;
import com.sanhe.a448.utils.Const;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class TimeActivity extends BaseWorkActivity {

    ActivityTimeBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTimeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> Const.sendTouchEvent(0x83, 0x10, 0xc, 0xc1, 0x1b));

        binding.cet.clean.setOnClickListener(v -> Const.sendTouchEvent(0x83, 0x10, 0xf, 0xf1, 0x1e));
        binding.ret.clean.setOnClickListener(v -> Const.sendTouchEvent(0x83, 0x10, 0xf, 0xf1, 0x1d));
        binding.hsp.clean.setOnClickListener(v -> Const.sendTouchEvent(0x83, 0x10, 0xf, 0xf1, 0x1f));

        binding.cet.time.setOnClickListener(v -> Const.sendTouchEvent(0x83, 0x10, 0xf, 0xf1, 0x1b));
        binding.ret.time.setOnClickListener(v -> Const.sendTouchEvent(0x83, 0x10, 0xf, 0xf1, 0x1a));
        binding.hsp.time.setOnClickListener(v -> Const.sendTouchEvent(0x83, 0x10, 0xf, 0xf1, 0x1c));

        binding.cet.title.setText("CET");
        binding.ret.title.setText("RET");
        binding.hsp.title.setText("HSP");

        updateCetTime();
        updateRetTime();
        updateHspTime();

        onMessageEvent(Event.cetClean);
        onMessageEvent(Event.retClean);
        onMessageEvent(Event.hspClean);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CetClean event) {
        updateClean(binding.cet.clean, event.getValue() == 1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RetClean event) {
        updateClean(binding.ret.clean, event.getValue() == 1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HspClean event) {
        updateClean(binding.hsp.clean, event.getValue() == 1);
    }

    private void updateClean(ImageView view, boolean isClean) {
        if (isClean) {
            view.setImageResource(R.mipmap.clean);
        } else {
            view.setImageDrawable(null);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CetDayEvent event) {
        updateCetTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CetHourEvent event) {
        updateCetTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CetMinEvent event) {
        updateCetTime();
    }

    private void updateCetTime() {
        String str = String.format("%sd %sh %sm", Event.cetDay.toString(), Event.cetHour.toString(), Event.cetMin.toString());
        binding.cet.time.setText(str);
    }

    public void onMessageEvent(RetDayEvent event) {
        updateRetTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RetHourEvent event) {
        updateRetTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RetMinEvent event) {
        updateRetTime();
    }

    private void updateRetTime() {
        String str = String.format("%sd %sh %sm", Event.retDay.toString(), Event.retHour.toString(), Event.retMin.toString());
        binding.ret.time.setText(str);
    }

    public void onMessageEvent(HspDayEvent event) {
        updateHspTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HspHourEvent event) {
        updateHspTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HspMinEvent event) {
        updateHspTime();
    }

    private void updateHspTime() {
        String str = String.format("%sd %sh %sm", Event.hspDay.toString(), Event.hspHour.toString(), Event.hspMin.toString());
        binding.hsp.time.setText(str);
    }
}
